///////////////////////////////////////////////////////
// Application requirements
#include "Program Includes.h"

#include "../Norton Engine/Utilities/Informant.h"
#include <stdlib.h>
#include <stdio.h>
#include <DbgHelp.h>
#include <time.h>
#pragma comment(lib, "dbghelp.lib") 
#if _DEBUG
	#include <vld.h>
#endif
/////////////////////////////////////////////////////////

#include "../Norton Engine/EngineInstance.h"


// TEMP
#include "../Norton Engine/WindowClass.h"

LONG WINAPI DumpFunction(_EXCEPTION_POINTERS * pExceptionInfo)
{
	SYSTEMTIME* CurTime = new SYSTEMTIME;
	GetSystemTime(CurTime);

	char FileName[256] = {0};
	sprintf_s(FileName, "Goupe_DumpFile %d_%d_%d  %d_%d_%d.mdmp", CurTime->wMonth, CurTime->wDay, CurTime->wYear, CurTime->wHour, CurTime->wMinute, CurTime->wSecond); 
	HANDLE File = CreateFile(FileName, GENERIC_WRITE, FILE_SHARE_WRITE,
		NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

	if(File != INVALID_HANDLE_VALUE)
	{
		_MINIDUMP_EXCEPTION_INFORMATION EInfo;

		EInfo.ThreadId = GetCurrentThreadId();
		EInfo.ExceptionPointers = pExceptionInfo;
		EInfo.ClientPointers = NULL;
		MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), File, MiniDumpNormal, &EInfo, NULL, NULL);
		CloseHandle(File);
	}

	delete CurTime;
	return 0;
}


int APIENTRY WinMain(__in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in_opt int nCmdShow)
{
	// Setup Dump File
	SetUnhandledExceptionFilter(DumpFunction);

	// Seed the random number generator
	srand(unsigned(time(0)));

	// Create and initialize the engine
	EngineInstance Engine;

	if(Engine.Initialize(hInstance))
	{
		Informant::Log(LOCATION, "Initialization of game is complete, starting game loop now!");
		/*WindowClass C(1600, 1000, false);*/

		// Gameloop
		while(Engine.HeartBeat());
	}

	// Shutdown game
	Engine.Shutdown();

	return 0;
}